package net.jlibrary.curbparking.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.curbparking.action.vo.PictureGalleryVo;
import net.jlibrary.curbparking.domain.PictureGalleryType;
import net.jlibrary.curbparking.service.IPictureGalleryManager;

@RequestMapping("/picture")
@RestController
public class PictureGalleryAction {

	@Autowired
	private IPictureGalleryManager pictureGalleryManager;

	@RequestMapping(value = "/pictureGalleryAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/curbparking/pictureGalleryAdminView");
	}

	@RequestMapping(value = "/pictureAdminViewList", method = { RequestMethod.POST })
	public Pagination lists(PictureGalleryVo pictureGalleryVo, PaginationParam param) {
		Pagination pagination = pictureGalleryManager.listsPage(pictureGalleryVo, param);
		return pagination;
	}

	@RequestMapping(value = "/pictureFormInit", method = { RequestMethod.GET })
	public PictureGalleryVo addFormInit() {
		PictureGalleryVo pictureGalleryVo = new PictureGalleryVo();
		return pictureGalleryVo;
	}

	@RequestMapping(value = "/detailPicture", method = { RequestMethod.GET })
	public PictureGalleryVo detail(PictureGalleryVo pictureGalleryVo) {
		pictureGalleryVo = pictureGalleryManager.get(pictureGalleryVo);
		return pictureGalleryVo;
	}

	@RequestMapping(value = "/savePicture", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") PictureGalleryVo pictureGalleryVo, @Token String token,
			BindingResult result) {
		pictureGalleryManager.save(pictureGalleryVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/updatePicture", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") PictureGalleryVo pictureGalleryVo, @Token String token,
			BindingResult result) {
		pictureGalleryManager.update(pictureGalleryVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/deletePicture", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		PictureGalleryVo pictureGalleryVo = new PictureGalleryVo();
		pictureGalleryVo.setId(id);
		pictureGalleryManager.delete(pictureGalleryVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

//	@RequestMapping(value = "/parkJsonData", method = { RequestMethod.GET })
//	public List<ModelMap> parkJsonData() {
//		List<ParkingPlace> places = pictureGalleryManager.parkingPlaces();
//		List<ModelMap> maps = new ArrayList<ModelMap>();
//		for (ParkingPlace place : places) {
//			ModelMap map = new ModelMap();
//			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, place.getId());
//			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, place.getFullName());
//			maps.add(map);
//		}
//		return maps;
//	}

	@RequestMapping(value = "/typeJsonData", method = { RequestMethod.GET })
	public List<ModelMap> typeJsonData() {
		List<PictureGalleryType> galleryTypes = pictureGalleryManager.galleryTypes();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (PictureGalleryType galleryType : galleryTypes) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, galleryType.getId());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, galleryType.getName());
			maps.add(map);
		}
		return maps;
	}

}
